﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using BindingIEBrowser;
using BindingIEBrowserUI;
using EccHttpLib;
using WebHacker;
using log4net;
using mshtml;


namespace HttpTest
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.ScriptErrorsSuppressed = false;
            ILog[] gg = LogManager.GetCurrentLoggers();
            //webBrowser1.Document.
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindingIEBrowserUI.IEInstancesForm _ieInstancesForm = new IEInstancesForm();
            _ieInstancesForm.Show();
            //var vBindingIEBrowser = new BindingCore();
            //var vBindingIEBrowser = new BindingCore("http://home.intranet.genpact.com/portal/page?_pageid=968,6605909&_dad=portal&_schema=PORTAL");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //String str = "管理番号&#9;&#9;：[01C012092900893]&#13;&#10;カテゴリ　&#9;&#9;：[【新規登録エラー】]&#13;&#10;ステータス&#9;&#9;：[未処理]";
            //int idx_start = str.IndexOf("【");
            //int idx_end = str.IndexOf("】");

            //str = str.Substring(idx_start, idx_end - idx_start);

            IEInstance _ieInstance = new IEInstance();
            IDictionary<string,string> _dictionary = 
            _ieInstance.GetInfo();


            foreach (var vle in _dictionary)
            {
                if ("TEXTAREAQUESTION".Equals(vle.Key))
                {
                    this.qText = vle.Value;
                }
                MessageBox.Show(vle.Key + ":" + vle.Value);
            }

            //_ieInstance.setAnswer("test answer");

            //_ieInstance.setMemo("test memory");
        }

        private void button3_Click(object sender, EventArgs e)
        {

            Thread td = new Thread(new ThreadStart(testThread));
            td.Start();
            webBrowser1.Url = new Uri(Application.StartupPath+"\\a1\\Item\\Menu.htm");
            //webBrowser1.Url = new Uri(Application.StartupPath + "\\a1\\MainHtml.htm");
            initFlg = false;
            //webBrowser1.
        }
        
        delegate  void d1();
        private void testThread()
        {
            while (pCheckResult == null)
            {
                Thread.Sleep(500);
            }

            this.Invoke(new d1(testDelegate));

        }

        private void testDelegate()
        {
            this.label1.Text = this.pCheckResult.idName;
            MessageBox.Show(
                pCheckResult.idName
                + "\n idNo:" +
                pCheckResult.idNo
                + "\n idAdd:" +
                pCheckResult.idAdd
                + "\n idTel:" +
                pCheckResult.idTel
                + "\n idAdd2:" +
                pCheckResult.idAdd2
                + "\n idTel2:" +
                pCheckResult.idTel2
                + "\n idName0:" +
                pCheckResult.idName0
                + "\n idNo0:" +
                pCheckResult.idNo0
                + "\n idName1:" +
                pCheckResult.idName1
                + "\n idNo1:" +
                pCheckResult.idNo1
                + "\n idName2:" +
                pCheckResult.idName2
                + "\n idNo2:" +
                pCheckResult.idNo2
                + "\n idName3:" +
                pCheckResult.idName3
                + "\n idNo3:" +
                pCheckResult.idNo3
                );
        }


        private string qText ="";
        private bool initFlg = false;
        private OnlineCheck oc;
        private CheckResult pCheckResult = null;
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            
            //ms
            //webBrowser1.Document.DomDocumen
            if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
                oc = new OnlineCheck(webBrowser1.Document.DomDocument);
                if(!initFlg)
                {
                    initFlg =
                    oc.CheckInfo(this.qText);
                    //MessageBox.Show("1");
                    pCheckResult = oc.GetProfile();
                    //MessageBox.Show("2");

                    //oc.test();


                    //oc.CheckInfo("asdfasdf");
                
                }
                
                //if(oc.IsCompleted(1))
                //{
                //    pCheckResult = oc.GetResult();
                //}
            }

            return;
            if(!initFlg)
            {
               // webBrowser1.Document.GetElementById("idInput").SetAttribute("value",this.qText);
                if((bool)webBrowser1.Document.InvokeScript("fnSelect") == false)
                {
                    MessageBox.Show("11");
                }
                initFlg = true;
                return;
            }else
            {
                MessageBox.Show(webBrowser1.Document.GetElementById("idMatch").InnerText);
                //html
                //id="idSNo"
            }



            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //ILog[] logs = LogManager.GetCurrentLoggers();

            //ILog log = LogManager.GetLogger("Test");

            ILog loge = LogManager.Exists("Test");

            //log.Logger. = true;
            loge.Info("I1");

            if(pCheckResult!=null)
            MessageBox.Show(
                pCheckResult.idName
                + "\n idNo:" +
                pCheckResult.idNo
                + "\n idAdd:" +
                pCheckResult.idAdd
                + "\n idTel:" +
                pCheckResult.idTel
                + "\n idAdd2:" +
                pCheckResult.idAdd2
                + "\n idTel2:" +
                pCheckResult.idTel2
                + "\n idName0:" +
                pCheckResult.idName0
                + "\n idNo0:" +
                pCheckResult.idNo0
                + "\n idName1:" +
                pCheckResult.idName1
                + "\n idNo1:" +
                pCheckResult.idNo1
+ "\n strCopy1:\n" +
pCheckResult.strCopy1
                + "\n idName2:" +
                pCheckResult.idName2
                + "\n idNo2:" +
                pCheckResult.idNo2
                + "\n strCopy2:\n" +
pCheckResult.strCopy2
                + "\n idName3:" +
                pCheckResult.idName3
                + "\n idNo3:" +
                pCheckResult.idNo3
                + "\n strCopy3:\n" +
pCheckResult.strCopy3
                );
        }

        private void button5_Click(object sender, EventArgs e)
        {


            WebTools wt  = new WebTools();
            String vLink = "http://svfile05/scripts/MDISS_WebUI/SKUInfo/INTSKU0/intSKU0.aspx";


            //MessageBox.Show(wt.HttpRequest_Text(vLink,new RequestHeaders()));

            mshtml.HTMLDocument hd =
            wt.HttpRequest(vLink,new RequestHeaders());

            //MessageBox.Show(hd.body.outerHTML);
            string tmpstr= "";

            tmpstr+=
            EccHttpLib.Utility.GetAttributeById(hd,"__VIEWSTATE", "value");
            tmpstr += "\n";

            tmpstr +=
            EccHttpLib.Utility.GetAttributeById(hd,"Hidden1", "value");
            tmpstr += "\n";

            tmpstr +=
EccHttpLib.Utility.GetAttributeById(hd,"txtSKU", "value");
            tmpstr += "\n";

//            tmpstr +=
//EccHttpLib.Utility.GetAttributeById("btnKensaku", "value");
//            tmpstr += "\n";

            tmpstr +=
EccHttpLib.Utility.GetAttributeById(hd,"txtMousikomiNum", "value");
            tmpstr += "\n";

            tmpstr +=
EccHttpLib.Utility.GetAttributeById(hd,"__EVENTVALIDATION", "value");
            tmpstr += "\n";

            MessageBox.Show(tmpstr);

            TestTest tt = new TestTest();
            tt.__VIEWSTATE = EccHttpLib.Utility.GetAttributeById(hd, "__VIEWSTATE", "value");
            tt.__EVENTVALIDATION = EccHttpLib.Utility.GetAttributeById(hd, "__EVENTVALIDATION", "value");
            tt.txtMousikomiNum = this.textBox1.Text;

            hd =
            wt.SubmitFormData(vLink, tt.ToString());
            HTMLDivElementClass hdec =
            hd.getElementById("MechandisePanel") as HTMLDivElementClass;

            HTMLTableRowClass  tmp1=
                hdec.firstChild.firstChild.firstChild as HTMLTableRowClass;
            HTMLTableCellClass tmp2 =
                tmp1.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild as HTMLTableCellClass;

            MessageBox.Show(tmp2.innerText);


        }


    }

    class TestTest:WebHacker.XMLHttpForm
    {
        public TestTest()
        {
            __VIEWSTATE = "";
            Hidden1 = "";
            txtSKU = "";
            btnKensaku = "";
            txtMousikomiNum = "";
            __EVENTVALIDATION = "";
        }


        public string __VIEWSTATE { get; set; }
        public string Hidden1 { get; set; }
        public string txtSKU { get; set; }
        public string btnKensaku { get; set; }
        public string txtMousikomiNum { get; set; }
        public string __EVENTVALIDATION { get; set; }

    }
}
